package com.zzyl.controller;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 护理项目Controller类
 */
@RestController
@RequestMapping("/nursing_project")
public class NursingProjectController extends BaseController {

    @Autowired
    private NursingProjectService nursingProjectService;

    @GetMapping
    public ResponseResult<PageResponse<NursingProjectVo>> getByPage(

            @RequestParam(value = "name", required = false) String name,

            @RequestParam(value = "status", required = false) Integer status,

            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,

            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageResponse<NursingProjectVo> page = nursingProjectService.getByPage(name, status, pageNum, pageSize);
        return ResponseResult.success(page);
    }

    @PostMapping
    public ResponseResult save(@RequestBody NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProjectService.save(nursingProject);
        return success();
    }

    @GetMapping("/{id}")
    public ResponseResult<NursingProjectVo> findById(@PathVariable(name = "id") Long id) {
        NursingProject nursingProject = nursingProjectService.findById(id);
        NursingProjectVo vo = BeanUtil.toBean(nursingProject, NursingProjectVo.class);
        return success(vo);

    }

    @PutMapping
    public ResponseResult update(@RequestBody NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProjectService.update(nursingProject);
        return success();
    }

    @PutMapping("/{id}/status/{status}")
    public ResponseResult updateStatus(@PathVariable(name = "id") Long id,
                                      @PathVariable(name = "status") Integer status) {
      nursingProjectService.updateStatus(id,status);
      return success();
    }
    @DeleteMapping("/{id}")
    public ResponseResult deleteById(@PathVariable(name="id") Integer id){
        nursingProjectService.deleteById(id);
        return success();

    }
}
